﻿using Api.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AppManager.Model
{
    /// <summary>
    /// 索引使用IndexAttribute
    /// </summary>
    public class App : BaseUserModel
    {
        public string _identity;
        /// <summary>
        /// 用户可以输入简单的文字，显示在url中
        /// </summary>
        [MaxLength(50)]
        [Index(IsUnique = true)]
        public string Identity
        {
            get { return _identity; }
            set { _identity = value?.Trim(); }
        }
        [MaxLength(50)]
        public string Name { get; set; }
        [MaxLength(100)]
        public string AppLogo { get; set; }

        [NotMapped]
        public List<AppInfo> AppInfos { get; set; }
        public AppInfo AndroidInfo
        {
            get
            {
                var defaultValue = new AppInfo { DeviceType = DeviceType.android };
                if (AppInfos != null)
                    return AppInfos.FirstOrDefault(s => s.DeviceType == DeviceType.android) ?? defaultValue;
                return defaultValue;
            }
        }
        public AppInfo IosInfo
        {
            get
            {
                var defaultValue = new AppInfo { DeviceType = DeviceType.ios };
                if (AppInfos != null)
                    return AppInfos.FirstOrDefault(s => s.DeviceType == DeviceType.ios) ?? defaultValue;
                return defaultValue;
            }
        }
        [MaxLength(20)]
        public string PassWord { get; set; }
        /// <summary>
        /// 保存和網址相關的绝对路径
        /// </summary>
        public string LogoUrl { get; set; }        
    }
    public class AppInfo : BaseUserModel
    {
        public string _testUrl;
        /// <summary>
        /// App表主键
        /// </summary>
        public int AppId { get; set; }
        [MaxLength(20)]
        public string Version { get; set; }
        public DateTime PushDate { get; set; }
        [MaxLength(20)]
        public string Size { get; set; }
        /// <summary>
        /// 正式发布地址
        /// </summary>
        [MaxLength(150)]
        public string Url { get; set; }
        /// <summary>
        /// 测试文件下载地址
        /// </summary>
        [MaxLength(150)]
        public string TestUrl
        {
            get
            {
                if (!FileHelper.FileExist(_testUrl))
                    _testUrl = string.Empty;
                return _testUrl;
            }
            set { _testUrl = value; }
        }
        public DeviceType DeviceType { get; set; }
        /// <summary>
        /// 应用简介
        /// </summary>
        [MaxLength(1000)]
        public string Description { get; set; }
        /// <summary>
        /// 更新说明
        /// </summary>
        [MaxLength(1000)]
        public string UpdatedExplain { get; set; }
        /// <summary>
        /// 瀏覽器打開App的協議地址
        /// </summary>
        public string SchemalUrl { get; set; }
    }
}
